package yxy.game.pm2.command.jadeseal;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.JadeSealMessage;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.jadeseal.JadeSeal;
import yxy.game.pm2.module.jadeseal.JadeSealModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;
import yxy.game.pm2.module.resonate.ResonateHandle;

import java.util.Collection;

/** 玉玺 阵营玉玺解锁 */
@MessageType(proto = Command.JadeSealUnlock, classtype = JadeSealMessage.JadeSealUnlockRequest.class)
public class CMD_JadeSealUnlock extends Command<JadeSealMessage.JadeSealUnlockRequest> {

	@Override
	public void execute(MessageSource messageSource, JadeSealMessage.JadeSealUnlockRequest request) {
		String playerCode = messageSource.getTag();
		int camp = request.getCamp();
		String herocode = request.getHerocode();
		JadeSealMessage.JadeSealUnlockResponse.Builder response = JadeSealMessage.JadeSealUnlockResponse.newBuilder();

		try {
			JadeSealModule jsmodule = JadeSealModule.get(playerCode);
			if(jsmodule == null) {
				send(messageSource, response, Result.DataError);
				return;
			}

			JadeSeal js = jsmodule.getJss().get(camp);
			if(js == null) {
				send(messageSource, response, Result.DataError);
				return;
			}

			if(js.isOpen()) {
				send(messageSource, response, Result.JadeSealHadUnlock);
				return;
			}

			Hero hero = Hero.get(playerCode, herocode);
			if(hero == null) {
				send(messageSource, response, Result.DataError);
				return;
			}

			if(hero.getStar() != 5) {
				send(messageSource, response, Result.DataError);
				return;
			}

			//共鸣的武将不能用作解锁
			Result resultForResonate = ResonateHandle.heroIsResonateHero(hero, playerCode);
			if(!resultForResonate.equals(Result.Success)){
				send(messageSource, response, resultForResonate);
				return;
			}

			//重生武将
			Collection<Currency> returnbakcurs = new HeroHandle().returnBack(playerCode, hero);
			Hero.removeHero(playerCode, hero, CCReason.玉玺);

			js.setOpen(true);
			js.setLevel(1);
			jsmodule.set();

			if(returnbakcurs != null) {
				new CurrencyHandle().addCurrency(playerCode, CCReason.玉玺, returnbakcurs);
				Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(returnbakcurs);
				response.addAllDisplay(items);
			}

			PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
			PlayTypeHandle.saveJadeSeal(playerCode,playTypeModule);//提前把战力算到中间缓存

			new BattleHandle().powerUpdate(playerCode);// 战力更新

			response.setCostherocode(herocode);
			send(messageSource, response, Result.Success);

			//判断如果消耗的是共鸣系统场上的武将，对应操作
			ResonateHandle.heroIsAboveHeroAndResolve(hero, playerCode);

		} catch (Exception e) {
			e.printStackTrace();
			send(messageSource, response, Result.DataError);

		}
	}

	private void send(MessageSource messageSource, JadeSealMessage.JadeSealUnlockResponse.Builder response, Result result) {
		response.setResult(result.packResult());
		MessageEmitterHandler.getInstance().send(messageSource, response.build());
	}
}